package com.toocms.jinjuhang.ui.main

import android.app.Application
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.JsonUtils
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.OrganizationMap
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.system.OrganizationMapData
import com.toocms.jinjuhang.widget.view_model.checkLoginStatus
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.Messenger
import com.toocms.tab.bus.event.SingleLiveEvent

class MainModel(application: Application) : BaseViewModel<BaseModel>(application) {

    init {
        Messenger.getDefault().register(this, Constants.MessengerToken.TOKEN_LIVE) {
            liveStreamingClickBindingCommand.execute()
        }
    }

    val showDialogEvent = SingleLiveEvent<Void>()

    val currentFilterObservableField = ObservableField<MainFilter>()
    val isShowDialog = ObservableBoolean()
    val image = ObservableField<String>()

    val contentSingleLiveEvent = SingleLiveEvent<MainFilter>()
    val indexClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        currentFilterObservableField.set(MainFilter.INDEX)
        contentSingleLiveEvent.value = MainFilter.INDEX
    })
    val classifyClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        currentFilterObservableField.set(MainFilter.CLASSIFY)
        contentSingleLiveEvent.value = MainFilter.CLASSIFY
    })
    val liveStreamingClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        currentFilterObservableField.set(MainFilter.LIVE_STREAMING)
        contentSingleLiveEvent.value = MainFilter.LIVE_STREAMING
    })
    val cartClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        currentFilterObservableField.set(MainFilter.CART)
        contentSingleLiveEvent.value = MainFilter.CART
    })
    val mineClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        currentFilterObservableField.set(MainFilter.MINE)
        contentSingleLiveEvent.value = MainFilter.MINE
    })
    val nullBindingCommand = BindingCommand<BindingAction>(BindingAction {
    })
    val closeCommand = BindingCommand<BindingAction>(BindingAction {
        showDialogEvent.call()
    })

    init {
        currentFilterObservableField.set(MainFilter.INDEX)
        contentSingleLiveEvent.value = MainFilter.INDEX
        initializeMessenger()
        check_alert()
//        organizationMap()
    }

    private fun initializeMessenger() {
        Messenger.getDefault().register(
            this,
            Constants.MessengerToken.TOKEN_MAIN_FILTER,
            MainFilter::class.java
        ) {
            if (MainFilter.CART == it && !checkLoginStatus()) return@register
            currentFilterObservableField.set(it)
            contentSingleLiveEvent.value = it
        }
    }

    private fun organizationMap() {
        requestData(
            Urls.System.ORGANIZATION_MAP,
            resultClass = OrganizationMapData::class.java,
            result = {
                OrganizationMap.organization.clear()
                if (!it.list.isNullOrEmpty()) {
                    OrganizationMap.organization.addAll(it.list)
                }
            })
    }

    private fun check_alert() {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        requestData(
            Urls.Center.CHECK_ALERT,
            params,
            resultClass = String()::class.java,
            result = {
                isShowDialog.set(JsonUtils.getString(it, "alert").equals("0"))
                image.set(JsonUtils.getString(it, "alert_pic"))
            })
    }

    fun close_alert() {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        requestData(
            Urls.Center.CLOSE_ALERT,
            params,
            resultClass = String()::class.java,
            result = {
            })
    }
}